package com.ads.proplan.activity;

import java.util.ArrayList;

import com.ads.proplan.R;
import com.ads.proplan.db.JogadorRepository;
import com.ads.proplan.db.entity.Jogador;
import com.googlecode.androidannotations.annotations.Click;
import com.googlecode.androidannotations.annotations.EActivity;
import com.googlecode.androidannotations.annotations.ViewById;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;

@EActivity(R.layout.menu_question)
public class MenuActivity extends Activity{
	
	public static final String TAG_SOM = "SOM";
	private SharedPreferences prefs;
	
	@ViewById(R.id.menu_radiobutton_som)
	public CheckBox checkBox;
	private boolean som;

	@Override
	protected void onStart() {
		super.onStart();
		checkBox.setVisibility(View.INVISIBLE);
		somChecked();
	}
	
	@Click(R.id.menu_button_estatistica)
	public void menuEstatisticas(){
		Intent intent = new Intent();
		intent.setClass(MenuActivity.this,ResultadosActivity.class);
		startActivity(intent);
	}
	
	@Click(R.id.menu_button_jogar)
	public void menuJogar(){
		SharedPreferences prefs = this.getSharedPreferences(QuestionActivity.TAG_SharedP, Context.MODE_PRIVATE);
		if (prefs.getBoolean(EndTimeActivity.TAG_DESAFIO, false)) {
			prefs.edit().putBoolean(EndTimeActivity.TAG_DESAFIO, false).commit();
			CharacterActivity_.intent(this).start();
		}else{
			Intent intent = new Intent();
			intent.setClass(MenuActivity.this,QuestionActivity_.class);
			startActivity(intent);
		}
		finish();
	}
	
	@Click(R.id.menu_button_sair)
	public void menuSair(){
		onBackPressed();
	}
	
	@Click(R.id.menu_button_informacao)
	public void menuInformacao(){
		Intent intent = new Intent();
		intent.setClass(MenuActivity.this,DuvidaArcada_.class);
		startActivity(intent);
	}
	
	@Click(R.id.cod)
	public void menuCod(){
		JogadorRepository jogadorRepos = new JogadorRepository(this);
		Jogador jogadorEntity = ((ArrayList<Jogador>) jogadorRepos.getAll()).get(0);
		if(jogadorEntity.getFase() == 1){
			jogadorEntity.setQCorretaRodada(3);
		}else if(jogadorEntity.getFase() == 2){
			jogadorEntity.setQCorretaRodada(6);
		}else{
			jogadorEntity.setQCorretaRodada(8);
		}
		jogadorRepos.update(jogadorEntity);
		Toast.makeText(getApplicationContext(),("Password Correto: "+jogadorEntity.getQCorretaRodada()), Toast.LENGTH_SHORT).show();
	}
	
	@Click(R.id.menu_button_configuracao)
	public void menuConfiguracao(){
		ConfiguracaoActivity_.intent(this).start();
	}
	
	@Click(R.id.menu_radiobutton_som)
	public void menuSom(){
		if(som){
			prefs.edit().putBoolean(TAG_SOM, false).commit();
			som = false;
			Toast.makeText(getApplicationContext(),("Som DESATIVADO"), Toast.LENGTH_SHORT).show();
		}else{
			prefs.edit().putBoolean(TAG_SOM, true).commit();
			som = true;
			Toast.makeText(getApplicationContext(),("Som ATIVADO"), Toast.LENGTH_SHORT).show();
		}
	}

	private void somChecked() {
		if (prefs == null) {
			prefs = this.getSharedPreferences(QuestionActivity.TAG_SharedP, Context.MODE_PRIVATE);
		}
		som = prefs.getBoolean(TAG_SOM, true);
		if(som){
			checkBox.setChecked(false);
		}else{
			checkBox.setChecked(true);
		}
	}
}
